package com.woniuxy.service.impl;

import com.woniuxy.domain.Product;
import com.woniuxy.service.IProductService;
import com.woniuxy.util.Constants;
import com.woniuxy.util.ResultVO;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ProductServiceFallbackFactory implements FallbackFactory<IProductService> {


    @Override
    public IProductService create(Throwable t) {

        // 这里可以获取到异常信息，至于如何处理，就由你决定了.
        log.warn("异常信息:{}", t.getMessage());
        return new IProductService() {
            @Override
            public ResultVO findById(Integer id) {
                Product product = new Product();
                product.setPid(-1);
                product.setPname("备用商品");
                product.setStock(100);
                log.error("此后备方法可以打印具体异常信息");
                return new ResultVO(Constants.OPER_SUCCESS, "后备方法findByPid执行了", product);
            }

            @Override
            public ResultVO reduceProductStock(Product product) {
                return null;
            }

            @Override
            public ResultVO addProduct(Product product) {
                return null;
            }

            @Override
            public ResultVO testLoadBalance() {
                return null;
            }
        };
    }
}
